package io.qytc.jboot.controller.system;

import com.jfinal.plugin.activerecord.Page;
import io.jboot.components.rpc.annotation.RPCInject;
import io.jboot.web.controller.annotation.RequestMapping;
import io.qytc.jboot.base.common.RestResult;
import io.qytc.jboot.base.exception.BusinessException;
import io.qytc.jboot.base.rest.datatable.DataTable;
import io.qytc.jboot.base.web.base.BaseController;
import io.qytc.jboot.service.api.VersionInfoService;
import io.qytc.jboot.service.entity.model.VersionInfo;


/**
 *  配置管理
 */

@RequestMapping("/system/versionInfo")
public class VersionInfoController extends BaseController {


    @RPCInject
    private VersionInfoService versionInfoService;

    public void index(){

        render("main.html");
    }

    public void tableData() {
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 30);

        VersionInfo search = new VersionInfo();

        Page<VersionInfo> dataPage = versionInfoService.findPage(search, pageNumber, pageSize);
        renderJson(new DataTable<VersionInfo>(dataPage));
    }

    public void add() {
        render("add.html");
    }

    public void postAdd() {
        VersionInfo versionInfo = getBean(VersionInfo.class, "versionInfo");

        try {
            versionInfoService.save(versionInfo);
        }catch (Exception e){
            throw new BusinessException("保存失败");
        }

        renderJson(RestResult.buildSuccess());
    }

    public void update() {
        int id = getParaToInt("id");
        VersionInfo versionInfo = versionInfoService.findById(id);
        if (null == versionInfo){
            throw new BusinessException("操作失败");
        }
        setAttr("versionInfo",versionInfo);
        render("update.html");
    }

    public void postUpdate() {
        VersionInfo versionInfo = getBean(VersionInfo.class, "versionInfo");

        if (!versionInfoService.update(versionInfo)) {
            throw new BusinessException("修改失败");
        }

        renderJson(RestResult.buildSuccess());
    }

    public void delete() {
        int id = getParaToInt("id");

        if (!versionInfoService.deleteById(id)) {
            throw new BusinessException("删除失败");
        }
        renderJson(RestResult.buildSuccess());
    }

}
